var util = require('../../../utils/util.js')
var config = require('../../../utils/config.js')
var API_URL = config.API_URL;
var page, loading, goodsTime;
/* 获取商品浏览历史列表 */
let getGood = that => {
    if (loading) {
        return
    }
    loading = true;
    util.PRL(API_URL.visitor_list, { page: page, action_type: 'views_goods' })
        .then(res => {
            console.log(res);
            let list, loadingText;
            let data = res.data.data;
            if (page == 1) {
                list = new Array();
            } else {
                list = that.data.good;
            }
            list = list.concat(data);
            if (res.data.hasMore == 0) {
                loadingText = '没有更多了';
            } else {
                loadingText = '加载中...'
            }
            page++
            that.setData({
                good: list,
                loadingText: loadingText
            })
            loading = false;
            goodsTime = res.data.data;
            var arr = []
            for (var i = 1; i < goodsTime.length; i++) {
                if (goodsTime[i].date == goodsTime[i - 1].date) {
                    arr.push("false")
                }
                if(goodsTime[i].date != goodsTime[i - 1].date){
                    arr.push("true")

                }

            }
        
            that.setData({
                arr:arr
            })
            console.log(that.data.arr[3])
            my.stopPullDownRefresh();
        }).catch(res => {
            console.log(res);
            loading = false;
            my.stopPullDownRefresh();
        })
}
/* 获取直播浏览历史列表 */
let getLive = that => {
    if (loading) {
        return
    }
    loading = true;
    util.PRL(API_URL.visitor_list, { page: page, action_type: 'watch_live' })
        .then(res => {
            console.log(res);
            let list, loadingText;
            let data = res.data.data;
            if (page == 1) {
                list = new Array();
            } else {
                list = that.data.live;
            }
            list = list.concat(data);
            if (res.data.hasMore == 0) {
                loadingText = '没有更多了';
            } else {
                loadingText = '加载中...'
            }
            if (data.length != 0) {
                page++
            }
            that.setData({
                live: list,
                loadingText: loadingText
            })
            loading = false;
            my.stopPullDownRefresh();
        }).catch(res => {
            console.log(res);
            loading = false;
            my.stopPullDownRefresh();
        })
}
Page({
    /**
  * 页面的初始数据
  */
    data: {
        tag: 'good',
        loadingText: '加载中...'
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        page = 1;
        loading = false;
        getGood(this);



    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onShow: function () {

    },
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */

    tag: function (e) {
        let that = this;
        let tag = e.currentTarget.dataset.tag;
        if (tag == that.data.tag) {
            return
        }
        that.setData({
            tag: tag,
            loadingText: '加载中...'
        })
        page = 1;
        if (tag == 'good') {
            getGood(that);
        } else {
            getLive(that);
        }
    },
    // 上拉加载
    onReachBottom: function () {
        let that = this;
        if (that.data.tag == 'good') {
            getGood(that);
        } else {
            getLive(that);
        }
    },

})